/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.service.impl;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.punish.csource.manage.mapper.CaseSourceDocumentRecordMapper;
import com.icinfo.cloud.provider.punish.csource.manage.model.CaseSourceDocumentRecord;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.dto.CaseTransferRecordFileDto;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.dto.CaseTransferSourceIdDto;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.mapper.CaseTransferRecordFileMapper;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.mapper.CaseTransferRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.model.CaseTransferRecordFile;
import com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.service.ICaseTransferRecordFileService;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import com.icinfo.framework.tools.utils.StringUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 描述: 案件移交材料证据表 case_transfer_record_file 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年12月09日
 */
@Service
public class CaseTransferRecordFileServiceImpl extends MyBatisServiceSupport implements ICaseTransferRecordFileService {

    @Resource
    private CaseTransferRecordFileMapper caseTransferRecordFileMapper;

    @Resource
    private CaseSourceDocumentRecordMapper caseSourceDocumentRecordMapper;

    @Resource
    private CaseTransferRecordMapper caseTransferRecordMapper;

    /**
     * 保存案件移交证据材料
     *
     * @param caseTransferRecordFileDto 案件移交材料参数对象
     * @return com.icinfo.framework.common.web.Result<java.lang.String>
     * @author liuyongfei
     * @date 2022/12/09
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result<String> saveCaseTransferRecordFile(CaseTransferRecordFileDto caseTransferRecordFileDto) {
        try {
            if (StringUtils.isBlank(caseTransferRecordFileDto.getCaseId()) && StringUtils.isBlank(
                    caseTransferRecordFileDto.getCaseSourceId())) {
                return Result.error("请传入案源id或案件id");
            }

            //查询是否已有移交记录
            Example deleteFileExample = new Example(CaseTransferRecordFile.class);

            Example example = new Example(CaseTransferRecordFile.class);
            Example.Criteria criteria = example.createCriteria();
            if (Constant.STRING_3.equals(caseTransferRecordFileDto.getTransferType())) {
                criteria.andEqualTo("caseSourceId", caseTransferRecordFileDto.getCaseSourceId());

                deleteFileExample.createCriteria().andEqualTo("caseSourceId", caseTransferRecordFileDto.getCaseSourceId());
            } else {
                criteria.andEqualTo("caseId", caseTransferRecordFileDto.getCaseId());
                deleteFileExample.createCriteria().andEqualTo("caseId", caseTransferRecordFileDto.getCaseId());
            }
            criteria.andEqualTo("deleted", Constant.STRING_0).andIsNotNull("sendUid");

            List<CaseTransferRecordFile> fileList = caseTransferRecordFileMapper.selectByExample(example);

            /*if (CollectionUtils.isNotEmpty(fileList)) {
                return Result.error("请勿重复移交！");
            }*/

            List<CaseTransferRecordFile> caseTransferRecordFileList = caseTransferRecordFileDto.getCaseTransferRecordFileList();
            if (CollectionUtils.isEmpty(caseTransferRecordFileList)) {
                return Result.error("材料证据不能为空！");
            }

            final Date date = new Date();

            //先删除再新增
            caseTransferRecordFileMapper.deleteByExample(deleteFileExample);

            caseTransferRecordFileList.forEach(caseTransferRecordFile -> {
                caseTransferRecordFile.setCaseId(caseTransferRecordFileDto.getCaseId());
                caseTransferRecordFile.setCaseSourceId(caseTransferRecordFileDto.getCaseSourceId());
                caseTransferRecordFile.setTransferType(caseTransferRecordFileDto.getTransferType());
                caseTransferRecordFile.setId(StringUtils.uuid());
                caseTransferRecordFile.setCreated(date);
                caseTransferRecordFile.setUpdated(date);
                caseTransferRecordFileMapper.insertSelective(caseTransferRecordFile);
            });
            return Result.success("保存成功！");
        } catch (Exception e) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return Result.error("保存失败！");
        }
    }

    /**
     * 查询案源文书记录
     *
     * @param caseTransferSourceIdDto 案源id
     * @return com.icinfo.framework.common.web.Result<java.util.List < com.icinfo.cloud.provider.punish.ucase.investigateevidence.transfer.model.CaseTransferRecordFile>>
     * @author liuyongfei
     * @date 2022/12/12
     */
    @Override
    public Result<List<CaseTransferRecordFile>> getSourceDocumentRecord(CaseTransferSourceIdDto caseTransferSourceIdDto) {
        Example example = new Example(CaseSourceDocumentRecord.class);
        example.createCriteria().andEqualTo("caseSourceId", caseTransferSourceIdDto.getCaseSourceId()).andEqualTo("documentRecordStatus",
                Constant.STRING_1);
        List<CaseSourceDocumentRecord> caseSourceDocumentRecordList = caseSourceDocumentRecordMapper.selectByExample(example);

        List<CaseTransferRecordFile> caseTransferRecordFileList = new ArrayList<>();

        for (CaseSourceDocumentRecord caseSourceDocumentRecord : caseSourceDocumentRecordList) {
            CaseTransferRecordFile caseTransferRecordFile = new CaseTransferRecordFile();
            caseTransferRecordFile.setCaseSourceId(caseTransferSourceIdDto.getCaseSourceId());
            caseTransferRecordFile.setFileName(caseSourceDocumentRecord.getDocumentNumber());
            caseTransferRecordFile.setFileUrl(caseSourceDocumentRecord.getDocumentUrl());
            caseTransferRecordFile.setTransferType(Constant.STRING_3);
            caseTransferRecordFile.setEvidenceCata(Constant.STRING_2);
            caseTransferRecordFile.setFileType("pdf");
            caseTransferRecordFile.setFileSuffix(".pdf");
            caseTransferRecordFileList.add(caseTransferRecordFile);
        }
        return Result.success(caseTransferRecordFileList);
    }
}